package com.kehutong.crm.message.service;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;
import com.kehutong.common.errcode.Result;
import com.kehutong.crm.message.entity.MessageAccountConfig;

public class MessageAccountConfigService {

    @Inject
    private JdbcSession jdbcSession;

    public MessageAccountConfig getAccountConfig(String uuid){
        MessageAccountConfig config = Pool.get(MessageAccountConfig.class, uuid);
        if (Objects.isNull(config)) {
            config = Pool.newInstance(MessageAccountConfig.class, uuid);
            config.setPreJob(true);
            config.setPreWorkOrder(true);
            config.setPreApprove(true);
            config.setSysNotice(true);
            jdbcSession.insert(config);
        }
        return config;
    }

    public Object updateAccountConfig(JSONObject jsonObject){
        String id = jsonObject.getString("id");
        if (Objects.isEmpty(id))
            return Result.error(0, "id是必传参数");

        MessageAccountConfig config = Pool.get(MessageAccountConfig.class, id);
        if (Objects.isNull(config))
            return Result.error(0, "不存在该配置，请检查参数后重试");

        Boolean preApprove = jsonObject.getBoolean("preApprove");
        if (Objects.nonNull(preApprove))
            config.setPreApprove(preApprove);

        Boolean preJob = jsonObject.getBoolean("preJob");
        if (Objects.nonNull(preJob))
            config.setPreJob(preJob);

        Boolean preWorkOrder = jsonObject.getBoolean("preWorkOrder");
        if (Objects.nonNull(preWorkOrder))
            config.setPreWorkOrder(preWorkOrder);

        Boolean sysNotice = jsonObject.getBoolean("sysNotice");
        if (Objects.nonNull(sysNotice))
            config.setSysNotice(sysNotice);

        jdbcSession.updateById(config);
        return Result.success();
    }
}
